package mysql

import (
	"fresh-box/freshrpc/freshrpc"
	"gorm.io/gorm"
)

type Fresh struct {
	gorm.Model
	Name    string `gorm:"type:varchar(20);not null;comment:名称"`
	Price   string `gorm:"type:varchar(20);not null;comment:价格"`
	TypesId int    `gorm:"type:varchar(20);not null;comment:类型id"`
}

func (f *Fresh) FreshAdd(db *gorm.DB) error {
	return db.Debug().Create(&f).Error
}

func (f *Fresh) GoodShow(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).First(&f).Error
}

func (f *Fresh) FreshList(db *gorm.DB, list []*freshrpc.FreshList, in *freshrpc.FreshListRequest) ([]*freshrpc.FreshList, error) {
	err2 := db.Debug().Model(&Fresh{}).
		Select("freshes.id as fresh_id",
			"freshes.name",
			"freshes.price",
			"freshes.types_id").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list).Error
	return list, err2
}
